package org.springcloud.wisdom.spring_01_canteen.dishes.fordishes;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.spring_01_canteen.login.entity.Mall;

import java.util.List;

@Mapper
public interface DishesDao {
    //查看所有菜品
    //分页查询
    @Select("<script>" +
            "SELECT * FROM wisdom.dishes" +
            "<where>" +
            "    <if test=\"dname != null\">" +
            " <bind name=\"Key\" value=\"'%'+dname+'%'\"/>"+
            "                        dname like #{Key} " +
            "    </if>" +
            "   <if test=\"mallid != null\">" +
            "      and mallid = #{mallid}" +
            "   </if>" +
            " and isdel = '已同意'"+
            "</where>" +
            "limit #{start},#{size}" +
            "</script>")
    List<Dishes> candish(int start, int size, String dname,Integer mallid);
    @Select("<script>" +
            "SELECT count(*) FROM wisdom.dishes" +
            "  <where>" +
            "    <if test=\"dname != null\">" +
            "      dname = #{dname}" +
            "    </if>" +
            "   <if test=\"mallid != null\">" +
            "      and mallid = #{mallid}" +
            "   </if>" +
            " and isdel = '已同意'"+
            "</where>"+
            "</script>")
    public int countdish(String dname,Integer mallid);

    //查看所有菜品
    //分页查询(2)
    @Select("<script>"
            + "SELECT dishes.*, mall.mname "
            + "FROM wisdom.dishes "
            + "JOIN wisdom.mall ON dishes.mallid = mall.mid "
            + "<where> "
            + "    <if test=\"dname != null and dname.trim() != ''\"> "
            + "        <bind name=\"Key\" value=\"'%'+dname+'%'\"/> "
            + "        dname like #{Key} "
            + "    </if> "
            + "</where> "
            + "ORDER BY CASE "
            + "           WHEN dishes.isdel = '未审核' THEN 0 "
            + "           ELSE 1 "
            + "          END, "
            + "        dishes.did ASC "
            + "LIMIT #{start}, #{size} "
            + "</script>")
    List<Dishes> candish1(@Param("start") int start, @Param("size") int size, @Param("dname") String dname);
    @Select("<script>" +
            "SELECT count(*) FROM wisdom.dishes" +
            "  <where>" +
            "    <if test=\"dname != null\">" +
            "      dname = #{dname}" +
            "    </if>" +

            "</where>"+
            "</script>")
    public int countdish1(String dname);


    @Select("<script>" +
            "SELECT * FROM wisdom.dishes" +
            "<where>" +
            "    <if test=\"dname != null\">" +
            " <bind name=\"Key\" value=\"'%'+dname+'%'\"/>"+
            "                        dname like #{Key} " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<Dishes> dishes(String dname);

    @Select("<script>"
            + "SELECT * FROM wisdom.dishes WHERE mallid = #{mallid}"+
            " and status = 1"
            + "</script>")
    List<Dishes> dishesByMallId(Integer mallid);
    //添加菜品
    @Insert("INSERT INTO wisdom.dishes (dname,dtype,dphotourl,dprice,dfood,mallid) values" +
            "(#{dname},#{dtype},#{dphotourl},#{dprice},#{dfood},#{mallid})")
    public int adddish(Dishes dishes);
    //修改菜品价格
    @Update("UPDATE wisdom.dishes " +
            "SET " +
            "dprice = #{dprice}" +
            "WHERE did = #{did}")
    public int updatedish(Dishes dishes);

    //更新菜品
    @Update("<script>" +
            "  update dishes" +
            "        <set>" +
            "            <if test=\"null != isdel\">" +
            "                isdel = #{isdel}" +
            "            </if>" +
            "            <if test=\"null!= dphotourl\">" +
            "                dphotourl = #{dphotourl}" +
            "            </if>" +
            "        </set>" +
            "        where did = #{did}" +
            "</script>")
    public int update(Dishes dishes);
    //删除菜品
    @Delete("DELETE FROM wisdom.dishes where did=#{did}")
    public int deldish(Integer did);
    //修改商品上下架状态
    @Update("UPDATE wisdom.dishes SET status = #{status} WHERE did = #{did}")
    int updateStatus(@Param("did") Integer did,@Param("status") Integer status);
    //修改商品审核状态
    @Update("UPDATE wisdom.dishes SET isdel = #{isdel} WHERE did = #{did}")
    int updateisdel(@Param("did") Integer did,@Param("isdel") String isdel);
    // 根据 mallid 计算菜品数量
    @Select("select count(*) from wisdom.dishes where mallid = #{mallid}")
    int countDishesByMallId(@Param("mallid") Integer mallid);
}
